//
//  DiaryCommentViewModel.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/2/9.
//

import Foundation
import SwiftUI

//历史回应
class HistoryCommentViewModel : BaseViewModel {
    @Published var changeSuc = false
    
    func change(bean: Binding<DiaryBean>, aiRoleId: String, text: String) {
        request {
            if bean.wrappedValue.isMultiple() {
                bean.wrappedValue.setNewChatAiAll(aiRoleId: aiRoleId, value: text)
            } else {
                let aiRole = try await BackupsManager.shared.getDelegateConfig()?.getAiRole(roleId: aiRoleId)
                bean.wrappedValue.setChatAiAll(aiRole: aiRole, value: text)
            }
            try await BackupsManager.shared.getDelegateDiary()?.saveDiary(bean: bean.wrappedValue)
        } suc: { _ in
            //改临时评论数据
            bean.wrappedValue.temOther?.first(where: { $0.aiRoleId == aiRoleId})?.content = text
//            NotificationCenter.default.post(name: .updateDiary, object: bean.wrappedValue)
            self.changeSuc = true
        } fail: {
            showToast($0.message)
        }
    }
}
